<?php
/*
* Author：鑫宇工作室
* QQ：639519801
* url：www.xinyuream.com
*/
$db=new lib_mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport);

function query($sql,$type=1){
	global $db;
	$re=$db->arr($sql,$type);
	if($type==1){
		return $re;
	}
}

if(!function_exists('getallheaders')){ 
	function getallheaders(){ 
		$headers = array (); 
		foreach ($_SERVER as $name => $value){ 
			if (substr($name, 0, 5) == 'HTTP_'){ 
				$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
			}
		}
		return $headers; 
	} 
}

class lib_mysqli {  
    protected $mysqli;                          //mysqli实例对象  
    //构造函数：主要用来返回一个mysqli对象  
    public function  __construct($dbhost, $dbuser, $dbpass, $dbname, $dbport) {  
        $this->mysqli = @new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport);  
        if(mysqli_connect_errno()) {  
            $this->mysqli    = false;  
            echo '<h2>'.mysqli_connect_error().'</h2>';  
            die();  
        } else {  
            $this->mysqli->set_charset("utf8");  
        }  
    } 
    //析构函数：主要用来释放结果集和关闭数据库连接  
    public function  __destruct() {  
        $this->free();  
        $this->close();  
    }  
  
    //释放结果集所占资源  
    protected function free() {  
        @$this->rs->free();  
    }  
  
    //关闭数据库连接  
    protected function close() {  
        $this->mysqli->close();  
    }
	public  function arr($sql, $type){
		$result = NULL;
        $query =  $this->mysqli->query($sql) or die ("ERROR:".$sql);
		switch ($type)
		{
			case 0 : 
				while (!!$row = mysqli_fetch_row($query)) { $result[] = $row; }
				break;
			case 1 :
				while (!!$row = mysqli_fetch_assoc($query)){ $result[] = $row; }
				 break;
			case 2 : $result = mysqli_affected_rows($this->mysqli); //返回 INERT UPDATE DELETE 響應行數
				break;
			case 3 : $result = mysqli_num_rows($query); 
				break;
			case 4 : $result = mysqli_insert_id($this->mysqli);
			break;

		}
		return $result;
	}
}





?>